from rest_framework import serializers
from orders.models import OrderInfo,OrderGoods
from goods.models import SKU

# sku表序列化器
class SKUSerializer(serializers.ModelSerializer):

    class Meta:
        # 指定根据那个模型类生成序列化器字段
        model = SKU
        # 指定那些字段生成
        fields = ('name','default_image')


# 订单商品表
class OrderGoodsSerializer(serializers.ModelSerializer):

    # sku表关联订单商品表
    sku = SKUSerializer()

    class Meta:
        # 指定根据那个模型类生成序列化器字段返回
        model = OrderGoods
        # 指定那些字段生成序列化器字段
        fields = ('count','sku','price')


# 订单表序列化器
class OrderSerializer(serializers.ModelSerializer):

    # 关联嵌套序列化返回
    user = serializers.StringRelatedField(read_only=True)
    address = serializers.PrimaryKeyRelatedField(read_only=True)

    # 订单商品表关联订单表
    skus = OrderGoodsSerializer(many=True)

    class Meta:
        # 指定根据那个模型类生成序列化器字段
        model = OrderInfo
        # 指定那些字段生成序列化器字段
        fields = '__all__'
